<%@ page language="java" import="java.util.*" pageEncoding="utf-8" isELIgnored="false"%>
<script type="text/javascript">
	var admin_zt_datagrid;
	var admin_zt_editRow = undefined;
	$(function() {
		admin_zt_datagrid = $('#admin_zt_datagrid').datagrid({
			url : '${pageContext.request.contextPath}/ztAction!dataGrid.action',
			fit : true,
			fitColumns : false,
			border : false,
			striped:true,
			rownumbers: true,//显示条数
			idField : 'id',
			sortOrder : 'asc',
			/*pagePosition : 'both',*/
			columns : [ [ {
				field : 'id',
				title : '状态编码',
				width : 150,
				checkbox: true//将显示的方式为checkbox 多选
			},  {
				field : 'zt',
				title : '状态名称',
				width : 250,
				editor : 'text'//将显示的方式为文本形式
			},{
					title : '操作权限',
					field : 'flag',
					width : 150,
					align : 'left',
					sortable:true,
					formatter:function(value){
						if(value == "T"){
							return "用户可操作";
						}else{
							return "用户不可操作";
						}
					},
					editor:{
						type:'combobox',
						options:{
							required:true,
							valueField:'flag',
							textField:'txtValue',
							data: [{
									txtValue: '用户可操作',
									flag: 'T'
								},{
									txtValue: '用户不可操作',
									flag: 'F'
								}],
							editable:false,
							missingMessage:'状态必填'
							
						}
					}
			
				},{
					title : '查看权限',
					field : 'flag1',
					width : 150,
					align : 'left',
					sortable:true,
					formatter:function(value){
						if(value == "T"){
							return "用户可见";
						}else{
							return "用户不可见";
						}
					},
					editor:{
						type:'combobox',
						options:{
							required:true,
							valueField:'flag1',
							textField:'txtValue',
							data: [{
									txtValue: '用户可见',
									flag1: 'T'
								},{
									txtValue: '用户不可见',
									flag1: 'F'
								}],
							editable:false,
							missingMessage:'状态必填'
							
						}
					}
			
				},{
					title : '处理状态',
					field : 'ztList',
					width : 150,
					align : 'left',
					sortable:true,
					formatter:function(value){
						if(value){
							return value[0];
						}
					},
					editor:{
						type:'comboboxFk',
						options:{
							valueField:'id',
							textField:'zt',
							url:'${pageContext.request.contextPath}/ztAction!comboboxInfo.action',
							editable:true,
							missingMessage:'货架编号必填'
							
						}
					}
			
				},{
					title : '状态图标',
					field : 'iconCls',
					width : 100,
					sortable:true,
					editor : {
					type : 'combobox',
					options : {
						valueField : 'iconCls',
						textField : 'iconCls',
						panelHeight : '200',
						url:'${pageContext.request.contextPath}/iconClsAction!comboboxInfo.action',
						formatter : function(v) {
							return sy.fs('<span class="{0}" style="display:inline-block;vertical-align:middle;width:16px;height:16px;"></span>{1}', v.iconCls, v.text);
						}
					}
				}}] ],
			toolbar : [ {
				text : '增加',
				iconCls : 'icon-add',
				handler : function() {
					if(!admin_zt_savePrompt()){
						$.messager.confirm('提示', '当前存在未保存项目，是否保存？', function(p) {
							if (p) {
								admin_zt_datagrid.datagrid('endEdit', admin_zt_editRow);
							}
						});
					}
					else
					admin_zt_add();
				}
			}, '-', {
				text : '删除',
				iconCls : 'icon-remove',
				handler : function() {
					admin_zt_del();
				}
			}, '-', {
				text : '修改',
				iconCls : 'icon-edit',
				handler : function() {
					admin_zt_edit();
				}
			}, '-', {
				text : '保存',
				iconCls : 'icon-save',
				handler : function() {
					if (admin_zt_editRow != undefined) {
						admin_zt_datagrid.datagrid('endEdit', admin_zt_editRow);
					}
				}
			}, '-', {
				text : '取消编辑',
				iconCls : 'icon-undo',
				handler : function() {
					admin_zt_datagrid.datagrid('unselectAll');
					admin_zt_datagrid.datagrid('rejectChanges');
					admin_zt_editRow = undefined;
				}
			}, '-', {
				text : '取消选中',
				iconCls : 'icon-undo',
				handler : function() {
					admin_zt_datagrid.datagrid('unselectAll');
				}
			},'-' ],
			onAfterEdit : function(rowIndex, rowData, changes) {
				var inserted = admin_zt_datagrid.datagrid('getChanges', 'inserted');
				var updated = admin_zt_datagrid.datagrid('getChanges', 'updated');
				if (inserted.length < 1 && updated.length < 1) {
					admin_zt_editRow = undefined;
					admin_zt_datagrid.datagrid('unselectAll');
					return;
				}

				var url = '';
				if (inserted.length > 0) {
					url = '${pageContext.request.contextPath}/ztAction!add.action';
				}
				if (updated.length > 0) {
					url = '${pageContext.request.contextPath}/ztAction!edit.action';
				}

				$.ajax({
					url : url,
					type:'post',
					data : rowData,
					dataType : 'json',
					success : function(r) {
						if (r.success) {
							admin_zt_datagrid.datagrid('acceptChanges');
							$.messager.show({
								msg : r.msg,
								title : '成功'		//从右下角跳出提示框显示成功
							});
							admin_zt_editRow = undefined;
							admin_zt_datagrid.datagrid('reload');
						} else {
							/*admin_zt_datagrid.datagrid('rejectChanges');*/
							admin_zt_datagrid.datagrid('beginEdit', admin_zt_editRow);
							$.messager.alert('错误', r.msg, 'error');
						}
						admin_zt_datagrid.datagrid('unselectAll');
					}
				});

			},
			onDblClickRow : function(rowIndex, rowData) {
				if (admin_zt_editRow != undefined) {
					admin_zt_datagrid.datagrid('endEdit', admin_zt_editRow);
				}

				if (admin_zt_editRow == undefined) {
					//changeEditoradmin_zt_editRow();/*改变editor*/
					admin_zt_datagrid.datagrid('beginEdit', rowIndex);
					admin_zt_editRow = rowIndex;
					admin_zt_datagrid.datagrid('unselectAll');
				}
			},
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});
	});
	
		function admin_zt_add() {
		if (admin_zt_editRow != undefined) {
			admin_zt_datagrid.datagrid('endEdit', admin_zt_editRow);
		}

		if (admin_zt_editRow == undefined) {
			admin_zt_datagrid.datagrid('unselectAll');
			
			admin_zt_datagrid.datagrid('insertRow', {
				index : 0,
				row : {
				
				}
			});
			admin_zt_editRow = 0;
			admin_zt_datagrid.datagrid('selectRow', 0);
			admin_zt_datagrid.datagrid('beginEdit', 0);
		}
	}
	function admin_zt_del() {
		if (admin_zt_editRow != undefined) {
			admin_zt_datagrid.datagrid('endEdit', admin_zt_editRow);
			return;
		}
		var ztbool = true;
		var rows = admin_zt_datagrid.datagrid('getSelections');
		var ids = [];
		if (rows.length > 0) {
			$.messager.confirm('请确认', '您要删除当前所选项目？', function(r) {
				if (r) {
					for ( var i = 0; i < rows.length; i++) {
						if(rows[i].id == '1' || rows[i].id == '2' || rows[i].id == '3' || rows[i].id == '4' || rows[i].id == '5' || rows[i].id == '6' || rows[i].id == '7' || rows[i].id == '8'){
							$.messager.alert('提示', rows[i].zt+'为系统内置状态,不可删除!', 'error');
							ztbool = false;
							break;
						}
						ids.push(rows[i].id);//将选中的ID值赋值给ids 用逗号分隔 ---
					}
					if(ztbool){
					$.ajax({
						url : '${pageContext.request.contextPath}/ztAction!delete.action',
						data : {
							ids : ids.join(',')
						},
						dataType : 'json',
						success : function(r) {
							if (r.success) {
									admin_zt_datagrid.datagrid('acceptChanges');
									$.messager.show({
										msg : r.msg,
										title : '成功'
									});
									admin_zt_editRow = undefined;
									admin_zt_datagrid.datagrid('clearSelections');
									admin_zt_datagrid.datagrid('load');
								} else {
									admin_zt_datagrid.datagrid('rejectChanges');
									$.messager.alert('错误', r.msg, 'error');
								}
						}
					});
					}
				}
			});
		} else {
			$.messager.alert('提示', '请选择要删除的记录！', 'error');
		}
	}
	function admin_zt_edit() {
		var rows = admin_zt_datagrid.datagrid('getSelections');
		if (rows.length == 1) {
			if (admin_zt_editRow != undefined) {
				admin_zt_datagrid.datagrid('endEdit', admin_zt_editRow);
				admin_zt_editRow = admin_zt_datagrid.datagrid('getRowIndex', rows[0]);
				admin_zt_datagrid.datagrid('beginEdit', admin_zt_editRow);
				admin_zt_datagrid.datagrid('unselectAll');
			}

			if (admin_zt_editRow == undefined) {
				//changeEditoradmin_zt_editRow();/*改变editor*/
				admin_zt_editRow = admin_zt_datagrid.datagrid('getRowIndex', rows[0]);
				admin_zt_datagrid.datagrid('beginEdit', admin_zt_editRow);
				admin_zt_datagrid.datagrid('unselectAll');
			}
		} else {
			$.messager.show({
				msg : '请选择一项进行修改！',
				title : '错误'
			});
		}
	}
	function admin_zt_save() {
		var inserted = admin_zt_datagrid.datagrid('getChanges', 'inserted');
		var updated = admin_zt_datagrid.datagrid('getChanges', 'updated');
		if (inserted.length < 1 && updated.length < 1) {
			admin_zt_editRow = undefined;
			admin_zt_datagrid.datagrid('unselectAll');
			return;
		}

		var url = '';
		if (inserted.length > 0) {
			url = 'ztAction!add.action';
		}
		if (updated.length > 0) {
			url = 'ztAction!edit.action';
		}
		var rowData = admin_zt_datagrid.datagrid('getData');
		console.info(rowData);

		$.ajax({
			url : url,
			type:'post',
			data : rowData,
			dataType : 'json',
			success : function(r) {
				if (r.success) {
					admin_zt_datagrid.datagrid('acceptChanges');
					$.messager.show({
						msg : r.msg,
						title : '成功'
					});
					admin_zt_editRow = undefined;
					admin_zt_datagrid.datagrid('reload');
				} else {
					/*admin_zt_datagrid.datagrid('rejectChanges');*/
					admin_zt_datagrid.datagrid('beginEdit', admin_zt_editRow);
					$.messager.alert('错误', r.msg, 'error');
				}
				admin_zt_datagrid.datagrid('unselectAll');
			}
		});
	}
	
	function admin_zt_savePrompt(){
		var inserted = admin_zt_datagrid.datagrid('getChanges', 'inserted');
		var updated = admin_zt_datagrid.datagrid('getChanges', 'updated');
		if (inserted.length < 1 && updated.length < 1) {
			return true;
		}
		return false;
	}
</script>

<div id="alluser_layout" class="easyui-layout" data-options="fit:true,border:false">
	<div data-options="region:'north'">
	</div>
	<div data-options="region:'center'">
		<table id="admin_zt_datagrid"></table>
	</div>
</div>
	

